package com.gk.carshopsys.service;

import com.gk.carshopsys.dao.AfterOrderDao;
import com.gk.carshopsys.dao.CarOrderDao;
import com.gk.carshopsys.dao.EmpDao;
import com.gk.carshopsys.pojo.AfterOrder;
import com.gk.carshopsys.pojo.AfterOrderImport;
import com.gk.carshopsys.pojo.CarOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AfterOrderServiceImpl implements AfterOrderService {
    @Autowired
    private AfterOrderDao afterOrderDao;
    @Autowired
    private EmpDao empDao;
    @Autowired
    private CarOrderDao carOrderDao;

    @Override
    public int addAfterOrderByList(List<AfterOrderImport> list) {

        Date date = new Date(System.currentTimeMillis());

//获取年月日
        java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
        sformat.applyPattern("yyyy");
        String year = sformat.format(date);
        sformat.applyPattern("MM");
        String mouth = sformat.format(date);
        sformat.applyPattern("dd");
        String day = sformat.format(date);


        for (AfterOrderImport afterOrderImport:list){
            int randow=(int)(Math.random()*(900+100));
            Long carid=carOrderDao.findCarOrderByOrdernum(afterOrderImport.getCarordernum()).get(0).getId();
            afterOrderImport.setCarid(carid);
            String ordernum =  year+mouth+day+afterOrderImport.getCarid()+afterOrderImport.getEmpid()+randow;
            afterOrderImport.setAfterordernum(ordernum);

        }

        return afterOrderDao.addAfterOrderByList(list);
    }

    @Override
    public List<CarOrder> findAfterOrder(HashMap<String, String> params) {
        return afterOrderDao.findAfterOrder(params);
    }

    /**
     *
     * @param carorder
     * @param carno
     * @param empnum
     * @param service
     * @param price
     * @param state
     * @param remarks
     * @return
     * -1没有这个订单
     * -2订单未完成
     * -3员工编号为空
     */
    @Override
    public int addAfterOrder(String carorder, String carno, String empnum, String service, Integer price, String state, String remarks) {
        AfterOrder afterOrder =new AfterOrder();


        if (carOrderDao.findCarOrderByOrdernum(carorder).isEmpty()){
            return -1;
        }else{
            if(!carOrderDao.findCarOrderByOrdernum(carorder).get(0).getState().equals("已支付")){
                return -2;
            }
            Long carid=carOrderDao.findCarOrderByOrdernum(carorder).get(0).getId();
            afterOrder.setCarid(carid);
        }




        if (empDao.findEmpByEmpno(empnum).isEmpty()){
            return -3;
        }else{
            Integer empid=empDao.findEmpByEmpno(empnum).get(0).getId();
            afterOrder.setEmpid(empid);
        }

        afterOrder.setCarno(carno);
        afterOrder.setService(service);
        afterOrder.setPrice(price);
        afterOrder.setState(state);
        afterOrder.setAfterremarks(remarks);

        String now = new Date(System.currentTimeMillis()).toString();
        afterOrder.setCreate_time(now);
        afterOrder.setUpdate_time(now);

        Date date = new Date(System.currentTimeMillis());

        //获取年月日
        java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
        sformat.applyPattern("yyyy");
        String year = sformat.format(date);
        sformat.applyPattern("MM");
        String mouth = sformat.format(date);
        sformat.applyPattern("dd");
        String day = sformat.format(date);

        Long carid = carOrderDao.findCarOrderByOrdernum(carorder).get(0).getId();
        Integer empid=empDao.findEmpByEmpno(empnum).get(0).getId();
        //        生成3位随机数
        int randow=(int)(Math.random()*(900+100));
        String ordernum =  year+mouth+day+carid+empid+randow;
        afterOrder.setAfterordernum(ordernum);




        return afterOrderDao.addAfterOrder(afterOrder);
    }


    @Override
    public int updateAfterOrderById(AfterOrder afterOrder) {

        String now = new Date(System.currentTimeMillis()).toString();
        afterOrder.setUpdate_time(now);
        System.out.println(afterOrder);
        return afterOrderDao.updateAfterOrderById(afterOrder);
    }

    @Override
    public int delAfterOrder(List<Integer> afterorder) {
        return afterOrderDao.delAfterOrder(afterorder);
    }
}
